***************************************************
*** RUN REGS COMPARING HOSP TO NBRS WITHIN 50M ***
*** Last edited: 5/12/2021         		   	    ***
***************************************************

log using "${CodePath}/log/regressions_50m_$S_DATE.log", text replace
cap mkdir "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_50m/"
cap mkdir "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_50m/Event Studies/"
cap mkdir "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_50m/Event Studies/nearseg100_state_2__state/"
cap mkdir "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_50m/Event Studies/nearseg100_state_2__state/CI95/"

*--------------------------------------------------
* Merge data and run regressions
*--------------------------------------------------

local hospvars    "`hospvars' log_n_claims"

use "${DataPath}/within50m/pnlist_groups_50m.dta", clear
duplicates drop hospcomp_group_50 pn, force
expand 9
sort pn
bys pn hospcomp_group_50 : gen fyear = _n + 2006
// create a small dataset with relevant vars
preserve
	use "${DataPath}/derived/hospyear_0716_jk.dta", clear
	keep pn fyear dist_to_border jk_state_audit_in2011* audit_in2011* jk_RACregion_audit_in2011* `hospvars' nearseg100_state_2 nearseg100_state RACregion state 
	keep if fyear >= 2007 & fyear <= 2015
	tempfile temp
	save `temp'
restore

merge m:1 pn fyear using `temp', keep(1 3) gen(_jkmerge)


egen hospcomp_group_fyear = group(hospcomp_group_50 fyear)
egen state_splitid = group(state split_id)
egen state_nearseg100 = group(state nearseg100_state)



foreach hospvar of varlist `hospvars' {

		ivreghdfe  `hospvar' (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
	 			= jk_state_audit_in2011_X2007 jk_state_audit_in2011_X2008 jk_state_audit_in2011_X2009 jk_state_audit_in2011_X2011 jk_state_audit_in2011_X2012 jk_state_audit_in2011_X2013 jk_state_audit_in2011_X2014 jk_state_audit_in2011_X2015) ///
				,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)
		iv_event_study95 audit_in2011
		graph export "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_50m/Event Studies/nearseg100_state_2__state/CI95/`hospvar'_IV.png", replace

	// Standardized scale for log_n_claims
	if "`hospvar'" == "log_n_claims"{
		
		ivreghdfe `hospvar' (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
			 			= jk_state_audit_in2011_X2007 jk_state_audit_in2011_X2008 jk_state_audit_in2011_X2009 jk_state_audit_in2011_X2011 jk_state_audit_in2011_X2012 jk_state_audit_in2011_X2013 jk_state_audit_in2011_X2014 jk_state_audit_in2011_X2015) ///
						,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)

		iv_event_study95_yscale  audit_in2011 -0.06 0.06 0.03
		graph export "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_50m/Event Studies/nearseg100_state_2__state/CI95/log_`hospvar'_IV_yscale.png", replace

	}
		
} // hospvar



log close